// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as express from 'express';
import { logger } from './middleware/logger.middleware';
import {
  TransformInterceptorLog,
  TransformInterceptor,
} from './interceptor/transform.interceptor';
import {
  HttpExceptionFilter,
  HttpExceptionFilterLog,
} from './filter/http-exception.filter';
import { AllExceptionsFilter } from './filter/any-exception.filter';
import {
  SwaggerModule,
  DocumentBuilder,
  SwaggerDocumentOptions,
} from '@nestjs/swagger';
import { AccountModule } from './logical/account/account.module';
import { AccessoryModule } from './logical/accessory/accessory.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const port = 9011;
  app.use(express.json()); // For parsing application/json
  app.use(express.urlencoded({ extended: true })); // For parsing application/x-www-form-urlencoded
  // 监听所有的请求路由，并打印日志
  app.use(logger);
  // 使用拦截器打印出参
  app.useGlobalInterceptors(new TransformInterceptorLog());
  // app.useGlobalInterceptors(new TransformInterceptor());
  // app.setGlobalPrefix('/api');
  app.useGlobalFilters(new AllExceptionsFilter());
  app.useGlobalFilters(new HttpExceptionFilterLog());
  app.useGlobalFilters(new HttpExceptionFilter());
  // 配置 Swagger
  const options: SwaggerDocumentOptions = {
    deepScanRoutes: true,
  };
  const config = new DocumentBuilder()
    .addApiKey(
      { type: 'apiKey', in: 'header', name: 'Authorization' },
      'Authorization',
    )
    .setTitle('系统')
    .setDescription('nest+sequelize+jwt')
    .setVersion('1.0')
    .addTag('登录验证相关接口', '获取用户信息，登录，登出，修改密码')
    .addTag('角色权限相关接口', '查询角色')
    .addTag('菜单相关接口', '获取角色菜单')
    .addTag(
      '组织机构相关接口',
      '新建，更新，删除，取得列表，取得详细，取得机构树',
    )
    .addTag('用户账号管理', '查询列表，查询详细，新建，修改，重置密码')
    .addTag('操作日志', '查询列表')
    .addTag('附件相关接口', '上传')
    .addTag('登录日志', '查询登录日志列表')
    .addTag('系统码表管理', '添加，修改，删除，查询详细')

    .build();
  const document = SwaggerModule.createDocument(app, config, options);
  SwaggerModule.setup('/swagger-html', app, document);

  await app.listen(port);
  console.log('http://localhost:' + port + '/swagger-html');
}
bootstrap();
